import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from '@/router'

export const useOffSetTopStore = defineStore(
  'offsetTop',
  () => {
    // 跳转------------
    const goHome = () => {
      router.push('./HomePage')
      window.scrollTo(0, 0)
    }

    const goPage = async (offsetTop) => {
      // 因为直接获取仓库内的数据会由于声明周期问题导致无法获取到数据，所以还得从外部传进来
      // 因为有个固定显示的导航栏，所以得减去75，滚动距离是盒子距离顶部距离，导航栏占位之后看上去就没有跳转对
      await router.push('./HomePage')
      document.documentElement.scrollTop = offsetTop - 75
    }

    // 距头部距离-------------
    // 关于我们
    const AoffsetTop = ref(0)
    // 新闻资讯
    const NoffsetTop = ref(0)
    // 知识库
    const KoffsetTop = ref(0)
    // 联系我们
    const CoffsetTop = ref(0)
    const setOffSetTop = (name, number) => {
      switch (name) {
        case 'A':
          AoffsetTop.value = number
          break
        case 'N':
          NoffsetTop.value = number
          break
        case 'K':
          KoffsetTop.value = number
          break
        default:
          CoffsetTop.value = number
      }
    }
    return {
      // 跳转
      goHome,
      goPage,
      // 距离
      AoffsetTop,
      NoffsetTop,
      KoffsetTop,
      CoffsetTop,
      setOffSetTop
    }
  },
  {
    persist: true
  }
)
